package com.canaan.business.controller.pc.agv;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.canaan.business.domain.agv.AgvTaskDispatch;
import com.canaan.business.domain.vo.AgvResultVo;
import com.canaan.business.domain.vo.AgvTaskCallbackVo;
import com.canaan.business.domain.vo.BillAgvVo;
import com.canaan.business.service.agv.IAgvTaskDispatchService;
import com.canaan.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.domain.R;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

/**
 * AGV任务调度Controller
 *
 * @author
 * @date 2023-03-02
 */
@Api(tags = "AGV任务调度")
@RestController
@RequestMapping("/wcs/agvTaskDispatch")
public class AgvTaskDispatchController extends BaseController {
    @Resource
    private IAgvTaskDispatchService agvTaskDispatchService;

    /**
     * 查询AGV任务调度列表
     */
    @ApiOperation(value = "查询AGV任务调度列表")
    @PreAuthorize("@ss.hasPermi('wcs:agvTaskDispatch:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgvTaskDispatch agvTaskDispatch) {
        startPage();
        List<AgvTaskDispatch> list = agvTaskDispatchService.selectAgvTaskDispatchListOrderByIdDesc(agvTaskDispatch);
        return getDataTable(list);
    }

    /**
     * 导出AGV任务调度列表
     */
    @ApiOperation(value = "导出AGV任务调度列表")
    @PreAuthorize("@ss.hasPermi('wcs:agvTaskDispatch:export')")
    @Log(title = "AGV任务调度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AgvTaskDispatch agvTaskDispatch) {
        List<AgvTaskDispatch> list = agvTaskDispatchService.selectAgvTaskDispatchListOrderByIdDesc(agvTaskDispatch);
        ExcelUtil<AgvTaskDispatch> util = new ExcelUtil<AgvTaskDispatch>(AgvTaskDispatch.class);
        util.exportExcel(response, list, "AGV任务调度数据");
    }

    /**
     * 获取AGV任务调度详细信息
     */
    @ApiOperation(value = "获取AGV任务调度详细信息")
    @PreAuthorize("@ss.hasPermi('wcs:agvTaskDispatch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(agvTaskDispatchService.selectAgvTaskDispatchById(id));
    }

    /**
     * 新增AGV任务调度
     */
    @ApiOperation(value = "新增AGV任务调度")
    @PreAuthorize("@ss.hasPermi('wcs:agvTaskDispatch:add')")
    @Log(title = "AGV任务调度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgvTaskDispatch agvTaskDispatch) {
        return toAjax(agvTaskDispatchService.insertAgvTaskDispatch(agvTaskDispatch));
    }

    /**
     * 修改AGV任务调度
     */
    @ApiOperation(value = "修改AGV任务调度")
    @PreAuthorize("@ss.hasPermi('wcs:agvTaskDispatch:edit')")
    @Log(title = "AGV任务调度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgvTaskDispatch agvTaskDispatch) {
        return toAjax(agvTaskDispatchService.updateAgvTaskDispatch(agvTaskDispatch));
    }

    /**
     * 删除AGV任务调度
     */
    @ApiOperation(value = "删除AGV任务调度")
    @PreAuthorize("@ss.hasPermi('wcs:agvTaskDispatch:remove')")
    @Log(title = "AGV任务调度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(agvTaskDispatchService.deleteAgvTaskDispatchByIds(ids));
    }

    /**
     * AGV任务回调
     *
     * @param objvo 任务回调实体
     * @return AgvDispatchResponse
     * @author
     */
    @PostMapping("/agvCallback")
    @ApiOperation(value = "AGV任务通知", notes = "AGV任务通知")
    @Transactional
    public AgvResultVo agvCallback(
            @ApiParam(name = "json_model")
            @RequestBody @Validated Object objvo) {
        AgvTaskCallbackVo agvTaskCallbackVo = BeanUtil.toBean(objvo, AgvTaskCallbackVo.class);
        return agvTaskDispatchService.agvCallback(agvTaskCallbackVo);
    }

    @PostMapping("/finishAgvCallback")
    @ApiOperation(value = "AGV手动完成", notes = "AGV手动完成")
    @Transactional
    public AgvResultVo finishAgvCallback(@ApiParam(name = "json_model")
                                             @RequestBody @Validated BillAgvVo vo){
        return agvTaskDispatchService.finishAgvCallback(vo);
    }

    /**
     * 取消任务
     *
     * @return com.canaan.common.core.util.R
     * @author
     */
//    @DeleteMapping("/{id}")
//    @ApiOperation(value = "取消AGV任务", notes = "取消AGV任务")
//    public R delete(
//            @ApiParam(name = "id", value = "id")
//            @PathVariable Long id) {
//        return agvTaskDispatchService.cancelTask(id);
//    }

    @GetMapping("/cancelCarry")
    @ApiOperation(value = "取消AGV任务", notes = "取消AGV任务")
    public R cancelCarry(@RequestParam("no") String no){
        return agvTaskDispatchService.cancelTaskByNo(no);
    }

    @GetMapping("/getTaskByNo")
    @ApiOperation(value = "查询AGV任务", notes = "查询AGV任务")
    public R getTaskByNo(@RequestParam("no") String no){
        return R.ok(agvTaskDispatchService.getTaskByNo(no));
    }

    @PostMapping("/transferShelf")
    @ApiOperation(value = "调库任务", notes = "调库任务")
    public AjaxResult transferShelf(@RequestBody AgvTaskDispatch agvTaskDispatch) {
        return agvTaskDispatchService.transferShelf(agvTaskDispatch);
    }
}
